package com.example.shopspring.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static Date twentyFourHoursAgo() {
        return before(24);
    }

    /**
     * 获取一周前的开始时间
     *
     * @return
     */
    public static Date weekAgo() {
        return before(24 * 7);
    }

    /**
     * 获取一个月前的开始时间
     *
     * @return
     */
    public static Date monthAgo() {
        return before(24 * 30);
    }


    public static Date before(int hour) {
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        // 设置为多少小时前
        calendar.add(Calendar.HOUR_OF_DAY, -hour);
        // 获取24小时前的时间戳对应的Date对象
        Date beforeHour = calendar.getTime();
        return beforeHour;
    }

    public static String dateToString(String type, Date date) {
        SimpleDateFormat sdf = null;
        if (type.equals("hour")) {
            sdf = new SimpleDateFormat("HH");
        } else {
            sdf = new SimpleDateFormat("MM-dd");
        }
        String dateString = sdf.format(date);
        return dateString;
    }
}
